﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Dynamic;
using System.Text;

namespace Eleanor.Website.Utility
{
    public class UrlParameters : Dictionary<string, string>
    {
        public UrlParameters()
            : base(StringComparer.OrdinalIgnoreCase)
        {

        }

        public string this[string key]
        {
            get
            {
                if (!base.ContainsKey(key))
                {
                    base.Add(key, "");
                }
                return base[key];
            }
            set
            {
                if (!base.ContainsKey(key))
                {
                    base.Add(key, value);
                }
                else
                {
                    base[key] = value;
                }
            }
        }

        public string ToPathString()
        {
            if (Count <= 0)
            {
                return string.Empty;
            }
            StringBuilder sb = new StringBuilder();
            foreach (string key in Keys)
            {
                sb.AppendFormat("|{0}={1}", key, HttpUtility.UrlEncode(this[key]));
            }
            return sb.ToString(1, sb.Length - 1);
        }

        public string ToQueryString()
        {
            if (Count <= 0)
            {
                return string.Empty;
            }

            StringBuilder sb = new StringBuilder();
            foreach (string key in Keys)
            {
                sb.AppendFormat("&{0}={1}", key, HttpUtility.UrlEncode(this[key]));
            }
            return sb.ToString(1, sb.Length - 1);
        }

        public void FromPathString(string path)
        {
            FromString("|", path);
        }

        public void FromQueryString(string query)
        {
            FromString("&", query);
        }

        private void FromString(string split, string from)
        {
            if (!string.IsNullOrWhiteSpace(from))
            {
                string[] arr = from.Split(split[0]);
                foreach (string item in arr)
                {
                    string[] kv = item.Split("="[0]);
                    if (kv.Length > 1)
                    {
                        this[kv[0]] = kv[1];
                    }
                    else
                    {
                        this[kv[0]] = "";
                    }
                }
            }
        }
    }

    public static class UrlParametersExtension
    {
        public static string GetListSubjectPath(this UrlParameters urlParameters, string subject)
        {
            return string.Format("/painting/list/catalog={0}|subject={1}",
                    HttpUtility.UrlPathEncode(urlParameters["catalog"]), HttpUtility.UrlPathEncode(subject));
        }
        public static string GetListAuthorshipPath(this UrlParameters urlParameters, string authorship)
        {
            return string.Format("/painting/list/catalog={0}|authorship={1}",
                    HttpUtility.UrlPathEncode(urlParameters["catalog"]), HttpUtility.UrlPathEncode(authorship));
        }
        public static string GetListFramesPath(this UrlParameters urlParameters, string frames)
        {
            return string.Format("/painting/list/catalog={0}|frames={1}",
                    HttpUtility.UrlPathEncode(urlParameters["catalog"]), HttpUtility.UrlPathEncode(frames));
        }
    }

    public class UrlFacade
    {
        public static UrlParameters GetPaintingListCondition(string strCondition)
        {
            UrlParameters condition = new UrlParameters();
            condition.FromPathString(strCondition);
            return condition;
        }

        public static string GetPaintingListUrl(UrlParameters condition)
        {
            return condition.ToPathString();
        }

        public static string GetPaintingSearchUrl(UrlParameters condition)
        {
            return "/painting/search?catalog=" + HttpUtility.UrlEncode(Const.DefaultCatalogName);
        }
    }
}